<template>
<div>

  <div>
    <el-input style="width: 200px;"  placeholder="请输入真实姓名" v-model="realName" suffix-icon="el-icon-search"></el-input>
    <el-input style="width: 200px;margin:10px 5px"   placeholder="请输入学校" v-model="college" suffix-icon="el-icon-school"></el-input>
    <el-input style="width: 200px;margin:10px 5px" v-model="identity" placeholder="请输入身份证号" suffix-icon="el-icon-collection"></el-input>
    <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
    <el-button @click="reset">重置</el-button>
  </div>


  <div style="margin:10px 0">
    <el-button type="primary" @click="handleAdd">新增
      <i class="el-icon-circle-plus-outline" />
    </el-button>
    <el-popconfirm  title="确定批量删除吗？" @confirm="delBatch"
                    class="ml-5">
      <el-button type="danger" slot="reference">批量删除
        <i class="el-icon-remove-outline" />
      </el-button>
    </el-popconfirm>
  </div>


  <el-table :data="tableData" border stripe :header-cell-style="tableHeaderColor"
            @selection-change="handleSelectionChange">
    <el-table-column
        type="selection"
        width="55">
    </el-table-column>
    <el-table-column prop="id" label="ID" width="50">
    </el-table-column>
    <el-table-column prop="username" label="用户名" width="80" ></el-table-column>
    <el-table-column prop="role" label="角色" width="100">
      <template slot-scope="scope">
        <el-tag type="primary" v-if="scope.row.role==='ROLE_ADMIN'">管理员</el-tag>
        <el-tag type="warning" v-if="scope.row.role==='ROLE_TEACHER'">老师</el-tag>
        <el-tag type="success" v-if="scope.row.role==='ROLE_STUDENT'">学生</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="realName" label="真实姓名" width="85" ></el-table-column>
    <el-table-column prop="identity" label="身份证号"></el-table-column>
    <el-table-column prop="sex" label="性别" width="55"></el-table-column>
    <el-table-column prop="email" label="邮箱"></el-table-column>
    <el-table-column prop="college" label="学校"></el-table-column>
    <el-table-column prop="major" label="专业"></el-table-column>
    <el-table-column prop="phone" label="电话" width="120"></el-table-column>


    <el-table-column label="操作" width="300" align="center">
      <template slot-scope="scope">
<!--        <el-button type="success" @click="lookCourse(scope.row)" v-if="scope.row.role==='ROLE_TEACHER'">查看课程 <i class="el-icon-edit"></i></el-button>-->
<!--        <el-button type="success" @click="stuLookCourse(scope.row)" v-if="scope.row.role==='ROLE_STUDENT'">查看已选课程 <i class="el-icon-edit"></i></el-button>-->
        <el-button type="warning" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
        <el-popconfirm  title="确定删除吗？" @confirm="del(scope.row.id)"
                        class="ml-5">
          <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>

      </template>
    </el-table-column>
  </el-table>

  <div style="padding:10px 0">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>

  <el-dialog title="用户信息" :visible.sync="dialogFormVisible" top="50px" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="用户名" >
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色" >
        <el-select clearable v-model="form.role" placeholder="请选择角色" style="width: 100%">
          <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="真实姓名" >
        <el-input v-model="form.realName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" >
      <el-input v-model="form.identity" autocomplete="off"></el-input>
     </el-form-item>
      <el-form-item label="性别" >
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学校" >
        <el-input v-model="form.college" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="专业" >
        <el-input v-model="form.major" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" >
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话" >
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

<!--  <el-dialog title="课程信息" :visible.sync="courseVisible" top="50px" width="30%">-->
<!--    <el-table :data="courses">-->
<!--      <el-table-column prop="name" label="课程名称"></el-table-column>-->
<!--      <el-table-column prop="score" label="课程学分"></el-table-column>-->
<!--    </el-table>-->
<!--  </el-dialog>-->

<!--  <el-dialog title="已选课程信息" :visible.sync="stuCourseVisible" top="50px" width="30%">-->
<!--    <el-table :data="courses">-->
<!--      <el-table-column prop="name" label="课程名称"></el-table-column>-->
<!--      <el-table-column prop="score" label="课程学分"></el-table-column>-->
<!--    </el-table>-->
<!--  </el-dialog>-->
</div>
</template>

<script>
export default {
  name: "User",
  data(){
    return{
      courses:[],
      courseVisible:false,
      stuCourseVisible:false,
      tableData: [],
      total:0,
      pageNum:1,
      pageSize:10,

      realName:"",
      college:"",
      identity:"",

      options: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }],

      form:{},
      dialogFormVisible:false,
      multipleSelection:[],
      roles:[],
      user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")):{},
    }
  },
  created() {
    this.load()
  },
  methods:{
    lookCourse(row){
      this.courses=row.courses
      this.courseVisible=true
    },
    stuLookCourse(row){
      this.courses=row.stuCourses
      this.stuCourseVisible=true
    },
    handleAdd(){
      this.dialogFormVisible=true
      this.form={}
    },
    handleEdit(row){
      this.form=row
      this.dialogFormVisible=true
    },
    reset(){
      this.realName=""
      this.college=""
      this.identity=""
      this.pageNum=1
      this.load()
    },
    handleSelectionChange(val){
      console.log(val)
      this.multipleSelection=val
    },
    delBatch(){
      let ids=this.multipleSelection.map(v=>v.id)  //[ {},{},{}] => [1,2,3]
      this.request.post("/user/del/batch",ids).then(res=>{
        if(res.code==='200'){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error("批量删除失败")
        }
      })
    },
    load(){
      this.request.get("/user/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          realName:this.realName,
          college:this.college,
          identity:this.identity
        }
      }).then(res =>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })

      this.request.get("/role").then(res=>{
        this.roles=res.data
      })
    },
    search(){
      this.request.get("/user/page",{
        params:{
          pageNum:1,
          pageSize:this.pageSize,
          realName:this.realName,
          college:this.college,
          identity:this.identity
        }
      }).then(res =>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    del(id){
      this.request.delete("/user/"+id).then(res=>{
        if(res.code==='200'){
          this.$message.success("删除成功")
          this.load()
        }else{
          this.$message.error("删除失败")
        }
      })
    },
    save(){ //新增或修改
      this.request.post("/user",this.form).then(res =>{
        if(res.code==='200'){
          this.$message.success("保存成功")
          if(this.form.id===this.user.id){
            //修改自己
            this.user=this.form
            localStorage.setItem("user",JSON.stringify(this.user))
          }
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("保存失败")
          this.load()
        }
      })

    },
    cancel(){
      this.dialogFormVisible=false
      this.load()
    },

    tableHeaderColor({row,column,rowIndex,columnIndex}){
      return 'background-color:lightblue;color:#fff;font-size:15px;'
    },
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum=pageNum;
      this.load()
    }
  }
}
</script>

<style scoped>

</style>